package com.jxt.myapp.dao.imp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.jxt.myapp.dao.IAccountDao;
import com.jxt.myapp.domain.account.Authority;
import com.jxt.myapp.domain.account.Role;
import com.jxt.myapp.domain.account.User;
import com.jxt.myapp.domain.jqgrid.Criterion;
import com.jxt.myapp.domain.jqgrid.JqGridParameter;
import com.jxt.myapp.utils.JqGridUtils;
import com.jxt.myapp.utils.PaginationUtils;

@Repository("accountDao")
public class AccountDao implements IAccountDao {

	private static final Logger logger = LoggerFactory
			.getLogger(AccountDao.class);

	private SimpleJdbcTemplate jdbcTemplate;

	@Resource(name = "dataSource")
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	@Override
	public List<Authority> getAllAuthority(JqGridParameter jq) {
		String sql, sql1, sql2;

		sql1 = "SELECT id,name,url FROM acct_authority";
		sql2 = " order by " + jq.getSidx() + " " + jq.getSord();
		sql = sql1 + sql2;
		sql = PaginationUtils.paginationSQLForMySQL(sql, jq.getPage(),
				jq.getRows());
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Authority>(
				Authority.class));
	}

	@Override
	public long getAllAuthorityCount() {
		return jdbcTemplate
				.queryForLong("SELECT COUNT(id) FROM acct_authority");
	}

	@Override
	public List<Authority> getAllAuthorityByParameters(JqGridParameter jq) {
		String sql, sql1, sql2, sql3;
		List<Criterion> criteria = Collections.emptyList();
		criteria.addAll(JqGridUtils.generateSearchCriteriaFromFilters(jq
				.getFilters()));
		sql1 = "SELECT id,name,url FROM acct_authority";
		Object[] parameters = new Object[criteria.size()];
		int index = 0;
		sql2 = JqGridUtils.converToSql(criteria, "");
		for (int i = 0; i < criteria.size(); i++) {
			parameters[index] = criteria.get(i).getValue();
			index++;
		}
		Object[] paramEnd = new Object[index];
		sql3 = " order by " + jq.getSidx() + " " + jq.getSord() + "";
		System.arraycopy(parameters, 0, paramEnd, 0, index);
		sql = sql1 + sql2 + sql3;
		sql = PaginationUtils.paginationSQLForMySQL(sql, jq.getPage(),
				jq.getRows());

		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Authority>(
				Authority.class), paramEnd);
	}

	@Override
	public long getAllAuthorityByParametersCount(JqGridParameter jq) {
		String sql, sql1, sql2;
		sql1 = "SELECT COUNT(id) FROM acct_authority";
		List<Criterion> criteria = new ArrayList<Criterion>();

		criteria.addAll(JqGridUtils.generateSearchCriteriaFromFilters(jq
				.getFilters()));

		sql2 = JqGridUtils.converToSql(criteria, "");

		sql = sql1 + sql2;
		Object[] parameters = new Object[criteria.size()];
		int index = 0;
		for (int i = 0; i < criteria.size(); i++) {
			parameters[index] = criteria.get(i).getValue();
			index++;
		}
		Object[] paramsEnd = new Object[index];
		System.arraycopy(parameters, 0, paramsEnd, 0, index);
		return jdbcTemplate.queryForLong(sql, paramsEnd);
	}

	@Override
	public Authority getAuthorityById(String id) {
		String sql = "SELECT id,name,url FROM acct_authority WHERE id = ?";
		return jdbcTemplate.queryForObject(sql,
				new BeanPropertyRowMapper<Authority>(Authority.class),
				new Object[] { id });
	}

	@Override
	public int updateAuthority(Authority auth) {
		String sql = "UPDATE acct_authority SET NAME = :NAME , url = :url WHERE id = :id";
		return jdbcTemplate.update(sql, new BeanPropertyRowMapper<Authority>(
				Authority.class));
	}

	@Override
	public int addAuthority(Authority auth) {
		String sql = "INSERT INTO acct_authority(id,NAME,url)VALUES(:id,:NAME,:url) ";
		return jdbcTemplate.update(sql, new BeanPropertyRowMapper<Authority>(
				Authority.class));
	}

	@Override
	public int[] deleteAuthority(List<Authority> auth) {
		String sql = "DELETE FROM acct_authority WHERE id = ?";
		SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(auth.toArray());
		return jdbcTemplate.batchUpdate(sql, batch);
	}

	@Override
	public List<Role> getAllRole(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getAllRoleCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Role> getAllRoleByParameters(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getAllRoleCountByParameters(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Role getRoleById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int addRole(Role role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int udpateRole(Role role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int[] deleteRole(List<Role> roleList) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> getAllUser(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getAllUserCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<User> getAllUserByParameters(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getAllUserByParametersCount(JqGridParameter jq) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public User getUserById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User getUserByloginName(String loginName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int[] deleteUser(List<User> userList) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int[] addRoleAuthority(String roleId, String[] authIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteRoleAuthority(String roleId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int[] addUserRole(String userId, String[] roleIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteUserRole(String userId) {
		// TODO Auto-generated method stub
		return 0;
	}

}
